package com.module.base.widget.skin;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.DrawableRes;
import androidx.appcompat.widget.AppCompatEditText;

import com.module.base.utils.EditTextUtils;
import com.module.base.R;

import skin.support.content.res.SkinCompatResources;
import skin.support.widget.SkinCompatBackgroundHelper;
import skin.support.widget.SkinCompatSupportable;
import skin.support.widget.SkinCompatTextHelper;

/**
 * @author bsnl_yanxuwen
 * @date 2021/7/27 15:27
 * Description :
 * EditText 默认皮肤
 */
public class SkinEditText extends AppCompatEditText implements SkinCompatSupportable {

    private SkinCompatTextHelper mTextHelper;
    private SkinCompatBackgroundHelper mBackgroundTintHelper;

    public SkinEditText(final Context context) {
        super(context);
        initMy(context, null, 0);
    }

    public SkinEditText(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        initMy(context, attrs, 0);
    }

    public SkinEditText(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initMy(context, attrs, defStyleAttr);
    }

    private void initMy(Context context, AttributeSet attrs, int defStyle) {

        mBackgroundTintHelper = new SkinCompatBackgroundHelper(this);
        mBackgroundTintHelper.loadFromAttributes(attrs, defStyle);
        mTextHelper = SkinCompatTextHelper.create(this);
        mTextHelper.loadFromAttributes(attrs, defStyle);

        applyBorderColorResource();
    }


    private void applyBorderColorResource() {
        try {
            EditTextUtils.setCursorDrawableColor(this, SkinCompatResources.getColor(getContext(), R.color.bg_default));
            //系统
            if (mBackgroundTintHelper != null) {
                mBackgroundTintHelper.applySkin();
            }
            if (mTextHelper != null) {
                mTextHelper.applySkin();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void setBackgroundResource(@DrawableRes int resId) {
        super.setBackgroundResource(resId);
        if (mBackgroundTintHelper != null) {
            mBackgroundTintHelper.onSetBackgroundResource(resId);
        }
    }

    @Override
    public void setTextAppearance(int resId) {
        setTextAppearance(getContext(), resId);
    }

    @Override
    public void setTextAppearance(Context context, int resId) {
        super.setTextAppearance(context, resId);
        if (mTextHelper != null) {
            mTextHelper.onSetTextAppearance(context, resId);
        }
    }

    @Override
    public void setCompoundDrawablesRelativeWithIntrinsicBounds(
            @DrawableRes int start, @DrawableRes int top, @DrawableRes int end, @DrawableRes int bottom) {
        super.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
        if (mTextHelper != null) {
            mTextHelper.onSetCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
        }
    }

    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(
            @DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
        super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
        if (mTextHelper != null) {
            mTextHelper.onSetCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
        }
    }

    @Override
    public void applySkin() {
        applyBorderColorResource();
    }
}

